import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Created by zhourh on 2018/5/23.
 * 方案上和SelfDividingNumber没有本质区别，只是使用了到流
 */
public class SelfDividingNumber2 {

    public static void main(String[] args) {
        SelfDividingNumber selfDividingNumber = new SelfDividingNumber();
        long startTime = System.currentTimeMillis();
        System.out.println(selfDividingNumber.selfDividingNumbers(1, 10000));
        System.out.println("use " + (System.currentTimeMillis() - startTime) + "ms");
    }

    public List<Integer> selfDividingNumbers(int left, int right) {
        return IntStream.range(left, right + 1).filter(i -> isSelfDividingNumber(i)).boxed().collect(Collectors.toList());
    }

    private boolean isSelfDividingNumber(int i) {
        return (i+"").chars().allMatch(c -> (c != '0' && i % Character.getNumericValue(c) == 0));
    }
}
